$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'dev/member/list',
        datatype: "json",
        colModel: [			
        	{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			/*{ label: '商户编号', name: 'uid', index: 'uid', width: 80 }, 			
			{ label: '会员编号', name: 'openid', index: 'openid', width: 80 }, */	
        	{ label: '卡号', name: 'number', index: 'number', width: 40 }, 
			{ label: '姓名', name: 'name', index: 'name', width: 80 }, 
			{ label: '推荐人', name: 'parentName', index: 'parentName', width: 80 }, 
			{ label: '余额', name: 'money', index: 'money', width: 40 }, 		
			{ label: '推广佣金', name: 'recommendMoney', index: 'recommendMoney', width: 40 }, 		
			{ label: '联系方式', name: 'mobile', index: 'mobile', width: 80 }, 
			{ label: '入住次数', name: 'orderCount', index: 'mobile', width: 40 },
			{ label: '入住天数', name: 'orderDays', index: 'mobile', width: 40 },
			{ label: '类型', name: 'type', width: 20, formatter: function(value, options, row){
				if(value == '1'){
					return '<span class="label label-success">业务员</span>';
				}else{
					return '<span class="label label-primary">会员</span>';
				}
			}},
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 50 },
			{ label: '操作', name: 'createTime', width: 35, formatter: function(value, options, row){
				if(row.type == 1){
					return '<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.updateMemberType('+row.id+',\'0\',\'确认取消('+row.name+')为业务员吗？\')">取消业务员</a>';
				}else{
					return '<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.updateMemberType('+row.id+',\'1\',\'确认设置('+row.name+')为业务员吗？\')">设置业务员</a>';
				}
			}}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            name: null,
            number: null,
            mobile: null
        },
		showList: true,
		title: null,
		member: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.member = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.member.id == null ? "dev/member/save" : "dev/member/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.member),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "dev/member/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		updateMemberType: function(id,type,msg){
			confirm(msg, function(){
				var data = {id:id,type:type};
				$.get(baseURL + "dev/member/updateMemberType",data, function(r){
	                alert(r.msg);
	                vm.reload();
	            });
			})
			
		},
		getInfo: function(id){
			$.get(baseURL + "dev/member/info/"+id, function(r){
                vm.member = r.member;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'name': vm.q.name,'mobile':vm.q.mobile,'number':vm.q.number},
                page:page
            }).trigger("reloadGrid");
		}
	}
});